/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package src;

import Model.Song;
import static java.awt.Color.red;
import java.awt.Component;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTextField;

/**
 * Για τον έλεγχο της στήλης "τίτλος τραγουδιού" στη διαχείριση άλμπουμ
 * Ελέγχει το μέγιστο πλήθος χαρακτήρων και αν υπάρχει διπλότυπο
 * 
 */
public class MaxLenNoDupSongDefaultCellEditor extends MaxLenDefaultCellEditor{
    List<Song> songList;

    public MaxLenNoDupSongDefaultCellEditor(JTextField jtf, JLabel jl,int maxLen,JTable jt, int colIndex,  List<Song> songList) {
        super(jtf, jl, maxLen, jt, colIndex);
        this.songList = songList;
    }
     @Override
    public boolean stopCellEditing() {
         if (jt.getEditingColumn() != this.index )
             return  super.stopCellEditing();
        if (super.stopCellEditing2() == false)
            return false;
        int rowIndex = this.jt.getEditingRow();
        if (rowIndex < 0)
            return true;
        int listIndex = this.jt.convertRowIndexToModel(rowIndex);
         if( !this.textField.getText().equals(General.Empty))   
             for (int i = 0; i < this.songList.size(); i++) {
                 if (i != listIndex && this.textField.getText().toUpperCase().equals(this.songList.get(i).getSongTitle().toUpperCase())) {
                     this.textField.setBorder(BorderFactory.createLineBorder(red));
                     this.label.setForeground(red);
                     this.label.setText("Αυτό το τραγούδι υπάρχει ήδη");
                     return false;
                 }
             }
         else {
             stopCellEditing3();
             return true;
         }
        stopCellEditing3();
        return true;
    }

     public Component getTableCellEditorComponent(JTable table,
        Object value, boolean isSelected, int row, int column) {
        return super.getTableCellEditorComponent(
            table, value, isSelected, row, column);
    }

    @Override
    public void cancelCellEditing() {
        super.cancelCellEditing(); //To change body of generated methods, choose Tools | Templates.
    }
    
}
